//
//  SettingView.m
//  AppleMarket
//
//  Created by yangy on 31/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "SettingView.h"
// viewModel
#import "SettingViewModel.h"

@interface SettingView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *loginoutButton;
@property (nonatomic, strong) SettingViewModel *viewModel;
@end

@implementation SettingView

- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
       
        [self initUI];
        [self refershLoginButtonTitle];
        
        
        [[[kNotificationCenter rac_addObserverForName:kUserDidLoginNotification object:nil] throttle:1] subscribeNext:^(NSNotification * _Nullable x) {
            // 登录成功
            [self refershLoginButtonTitle];
        }];
        
        [[kNotificationCenter rac_addObserverForName:kUserDidLoginOutNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            [self refershLoginButtonTitle];
        }];
    }
    return self;
}

- (void) initUI
{
    [self addSubview:self.tableView];
    [self addSubview:self.loginoutButton];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.loginoutButton.mas_top);
    }];
    [_loginoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.height.mas_equalTo(46);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-24);
    }];
}

- (void) refershLoginButtonTitle
{
    NSString *title = [PlistCommon.loginModel UserID] != 0? @"退出登录" : @"去登录";
    [self.loginoutButton setTitle:title forState:UIControlStateNormal];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idx = @"";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:idx];
        cell.textLabel.font = kFont(15);
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.font = kFont(15);
        cell.detailTextLabel.textColor = kColorWithHex(0x80878E);
    }
    if(indexPath.row == 0){
        cell.textLabel.text = @"费率详情";
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"语言";
        cell.detailTextLabel.text = @"简体中文";
    }else{
        cell.textLabel.text = @"关于我们";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        // 费率
        [self.viewModel.refershRateCommand execute:nil];
    }else{
        [self.viewModel.cellClickSubhject sendNext:@(indexPath.row)];
    }
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
        [_tableView setScrollEnabled:NO];
        [_tableView setSeparatorColor:kColorWithHex(0xEFEFEF)];
        [_tableView setTableFooterView:[UIView new]];
    }
    return _tableView;
}

- (UIButton *)loginoutButton
{
    if(!_loginoutButton){
        _loginoutButton = [UIButton cm_ButtonWithTitle:@"" font:kFont(15) titleColor:kColorWithHex(0x3291EB)];
        _loginoutButton.layer.borderColor = kColorWithHex(0x3291EB).CGColor;
        _loginoutButton.layer.borderWidth = 1;
        [[_loginoutButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if([PlistCommon loginModel].UserID != 0){
                // 退出登录
                [self.viewModel.loginoutCommand execute:nil];
            }else{
                [self.viewModel.loginClickSubject sendNext:nil];
            }
        }];
    }
    return _loginoutButton;
}

@end
